package com.cds.drizzt.service;

import com.cds.drizzt.dao.UsersDao;
import com.cds.drizzt.entity.Users;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
@Slf4j
public class UserService implements UserDetailsService {
    @Autowired
    private UsersDao usersDao;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Users users = usersDao.getUserByUsername(username);
        if (users == null) {
            throw new UsernameNotFoundException("User not found");
        }

        return new org.springframework.security.core.userdetails.User(
            users.getUsername(),
            users.getPassword(),
            new ArrayList<>()
        );
    }

}
